import smtplib
from email.mime.text import MIMEText
from email.header import Header

mail_host = "smtp.163.com"  # 设置服务器
mail_user = "moyucf_top@163.com"
mail_pass = "EETLFDOIZSFCTAFV"

# 设置邮件发送者
sender = mail_user
# 设置邮件接收者
receivers = ['924592920@qq.com']  # 接收邮件

# 创建一个带附件的实例,发送内容：邮件发送测试...
message = MIMEText('邮件发送测试...', 'plain', 'utf-8')
# 设置发送者
message['From'] = Header(sender, 'utf-8')
# 设置接收者
message['To'] = Header(", ".join(receivers), 'utf-8')
# 设置邮件主题
subject = 'Python SMTP 邮件测试'
message['Subject'] = Header(subject, 'utf-8')

try:
    # 使用SMTP_SSL并连接到465端口（或根据您的服务器要求使用587并启用TLS）
    smtpObj = smtplib.SMTP_SSL(mail_host, 994)  # 对于需要SSL的服务器
    #smtpObj = smtplib.SMTP(mail_host, 25)  # 对于需要TLS的服务器，并在登录后调用smtpObj.starttls()
    smtpObj.login(mail_user, mail_pass)
    smtpObj.sendmail(sender, receivers, message.as_string())
    print("邮件发送成功")
    smtpObj.quit()  # 确保连接被正确关闭
except smtplib.SMTPException as e:
    import traceback
    print(f"发送邮件时发生异常: {str(e)}")
    traceback.print_exc()
